Path 1: 33 calls (0.77)

Text (33)

96 (12) 46 (6) 26 (3) 76 (3) 88 (3) 87 (2) 60 (1) 40 (1) 50 (1) 34 (1)

'center' (33)

'─' (33)

Style (33)

Text (33)

1def align_text(
2            text: Text, width: int, align: str, character: str, style: Style
3        ) -> Text:
4            """Gets new aligned text.
5
6            Args:
7                text (Text): Title or subtitle text.
8                width (int): Desired width.
9                align (str): Alignment.
10                character (str): Character for alignment.
11                style (Style): Border style
12
13            Returns:
14                Text: New text instance
15            """
16            text = text.copy()
17            text.truncate(width)
18            excess_space = width - cell_len(text.plain)
19            if excess_space:
20                if align == "left":
21                    return Text.assemble(
22                        text,
23                        (character * excess_space, style),
24                        no_wrap=True,
25                        end="",
26                    )
27                elif align == "center":
28                    left = excess_space // 2
29                    return Text.assemble(
30                        (character * left, style),
31                        text,
32                        (character * (excess_space - left), style),
33                        no_wrap=True,
34                        end="",
35                    )
36                else:
37                    return Text.assemble(
38                        (character * excess_space, style),
39                        text,
40                        no_wrap=True,
41                        end="",
42                    )
43            return text
            

Path 2: 10 calls (0.23)

Text (10)

46 (6) 6 (3) 65 (1)

'center' (10)

'─' (10)

Style (10)

Text (10)

1def align_text(
2            text: Text, width: int, align: str, character: str, style: Style
3        ) -> Text:
4            """Gets new aligned text.
5
6            Args:
7                text (Text): Title or subtitle text.
8                width (int): Desired width.
9                align (str): Alignment.
10                character (str): Character for alignment.
11                style (Style): Border style
12
13            Returns:
14                Text: New text instance
15            """
16            text = text.copy()
17            text.truncate(width)
18            excess_space = width - cell_len(text.plain)
19            if excess_space:
20                if align == "left":
21                    return Text.assemble(
22                        text,
23                        (character * excess_space, style),
24                        no_wrap=True,
25                        end="",
26                    )
27                elif align == "center":
28                    left = excess_space // 2
29                    return Text.assemble(
30                        (character * left, style),
31                        text,
32                        (character * (excess_space - left), style),
33                        no_wrap=True,
34                        end="",
35                    )
36                else:
37                    return Text.assemble(
38                        (character * excess_space, style),
39                        text,
40                        no_wrap=True,
41                        end="",
42                    )
43            return text